package uk.co.cdal.cobweb.frontend;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Toolkit;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;
import java.rmi.RemoteException;

import javax.swing.JFrame;
import javax.swing.JScrollPane;

import uk.co.cdal.cobweb.backend.BackendImpl;
import uk.co.cdal.cobweb.frontend.GUI.ChatItem;
import uk.co.cdal.cobweb.frontend.GUI.ChatPanel;
import uk.co.cdal.cobweb.frontend.GUI.DrawingPanel;
import uk.co.cdal.cobweb.frontend.GUI.MenuBar;

public class Frontend extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -3011060906363136613L;

	private ChatPanel cp;

	private DrawingPanel dp;

	private BackendImpl backend;
	
	private MenuBar mb;
	
	private static Frontend frontend;
	
	private Frontend() {
		//Nothing here just stop gap.
	}

	private Frontend(BackendImpl backend) {
		super("COBWEB - The Collaborative Spider Map (WEB) Development Suite");
		this.backend = backend;
		//init();

	}

	public void init() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setLayout(new BorderLayout());
		Container c = getContentPane();
		mb = new MenuBar();
		c.add(mb, BorderLayout.NORTH);
		cp = new ChatPanel();
		c.add(cp, BorderLayout.SOUTH);	
		cp.setVisible(false);
		dp = new DrawingPanel(backend);
		JScrollPane dpscroll = new JScrollPane(dp);
		dpscroll.setHorizontalScrollBarPolicy(JScrollPane.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		dpscroll.setVerticalScrollBarPolicy(JScrollPane.VERTICAL_SCROLLBAR_AS_NEEDED);
		c.add(dpscroll,BorderLayout.CENTER);
		this.setSize(800, 600);
		setIconImage(Toolkit.getDefaultToolkit().getImage("icons/chart_organisation.png"));
		setExtendedState(JFrame.MAXIMIZED_BOTH);
		setVisible(true);
		addKeyListener(new DrawingPanelKeyListener());
	}
	
	public static Frontend getFrontend() {
		if(frontend == null) {
			try {
				frontend = new Frontend(new BackendImpl());
			}
			catch(RemoteException e) {
				
			}
		}
		return frontend;
	}
	
	public static Frontend getFrontend(BackendImpl b) {
		if(frontend == null) {
			frontend = new Frontend(b);
		}
		return frontend;
	}
	
	public DrawingPanel getDrawingPanel()
	{
		return dp;
	}
	
	public Object clone() throws CloneNotSupportedException {
		throw new CloneNotSupportedException();
	}

	public void addChatItem(String from, String message, String date) {
		cp.addChatItem(new ChatItem(from, message, date));
	}

	public void newMessage() {
		backend.newMessage(cp.getInput());
		cp.clearInput();
	}
	
	public void newSysMessage(String message) {
		cp.addSystemMessage(message);
	}

	public void setUsername() {
		backend.setUsername("Lemon");
	}
	
	public void update() {
		dp.getIdeas();
	}
	
	public BackendImpl getBackend() {
		return backend;
	}
	
	public void showChat(boolean visible) {
		cp.setVisible(visible);
	}
	
	public boolean isChatVisible() {
		return cp.isVisible();
	}

	/**
	 * @return the mb
	 */
	public MenuBar getMb() {
		return mb;
	}
	
	public class DrawingPanelKeyListener extends KeyAdapter {
		
		public void keyPressed(KeyEvent e) {
			System.out.println(e.getKeyModifiersText(e.getModifiers()) + e.getKeyText(e.getKeyCode()));
			if(e.getKeyText(e.getKeyCode()).equals("N") && e.getKeyModifiersText(e.getModifiers()).equals("Ctrl")) {
				dp.addIdea(getWidth()/2, getHeight()/2);
			}
		}
	}
}
